exports.search_organization_course_detail = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var courseBrowsing = 0;

    var browseTimes = 0;

    //任务流创建
    async.waterfall([

            //1.查询机构课程
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_organization_course + "/oc_id/" + restfulParams.oc_id,
                        method: 'POST',
                        data: {
                            fields: ["oc_id", "oc_title", "oc_title_image", "oc_old_price", "oc_new_price", "oc_course_count", "oc_sales_volume", "oc_age", "oc_base", "oc_count", "oc_desc", "oc_type", "oc_cid", "oc_appointment", "oc_switching", "oc_refund", "oc_other_info", "oc_prompt", "oc_start_time", "oc_end_time"].join(",")
                        }
                    },
                    function (err, data) {
                        // //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationCourseData = data.data;
                        cb();
                    }
                );

            },

            //2.对应的机构
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + restfulParams.o_id,
                        method: 'POST',
                        data: {
                            fields: ["o_id", "o_name", "o_address", "o_longitude", "o_latitude", "o_phone", "o_slogan"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationData = data.data;

                        cb();
                    }
                );

            },


            //3.相关推荐
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationCourseInfo + "/oc_type/0/o_id/"+restfulParams.o_id+"/skip/0/limit/5",
                        method: 'POST',
                        data: {
                            fields: ["oc_id", "oc_title", "oc_title_image", "oc_course_count", "oc_sales_volume", "oc_new_price", "oc_oid", "o_name"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationRecommendData = data.data;
                        cb();
                    }
                );

            },
            //代金券
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_cash_coupon + "/cc_oid/" + restfulParams.o_id,
                        method: 'POST',
                        data: {
                            fields: ["cc_id", "cc_name", "cc_price", "cc_start_time", "cc_end_time"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationCouponCommentData = data.data;

                        cb();
                    }
                );

            },
            function (cb) {
                if (restfulParams.u_id) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.userSearchCollect + "/is_delete/0/uc_type/0/uc_fid/" + restfulParams.oc_id + "/uc_uid/" + restfulParams.u_id,
                            method: 'POST',
                            data: {
                                fields: ['uc_id'].join(",")
                            }

                        },
                        function (err, data) {

                            var is_zan = data.data;

                            if (is_zan.length <= 0) {
                                resultData.isCollectData = 0;
                            } else {
                                resultData.isCollectData = 1;
                            }
                            cb();
                        }
                    );
                } else {
                    cb();
                }
            },

            //查询用户是否浏览过
            function (cb) {
                if (restfulParams.u_id) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchOrganizationCourseBrowsingHistory + "/uocbh_ocid/" + restfulParams.oc_id + "/uocbh_uid/" + restfulParams.u_id,
                            method: 'POST',
                            data: {
                                fields: ['uocbh_id'].join(",")
                            }
                        },
                        function (err, data) {

                            courseBrowsing = data.data;
                            cb();
                        }
                    );
                } else {
                    cb();
                }
            },
            //查询浏览次数
            function (cb) {
                if (restfulParams.u_id) {
                    if (courseBrowsing.length > 0) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchOrganizationCourseBrowsingHistory + "/uocbh_ocid/" + restfulParams.oc_id + "/uocbh_uid/" + restfulParams.u_id,
                                method: 'POST',
                                data: {
                                    fields: ['uocbh_id','uocbh_count'].join(",")
                                }
                            },
                            function (err, data) {

                                browseTimes = data.data;

                                cb();
                            }
                        );
                    } else {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.addOrganizationCourseBrowsingHistory,
                                method: 'POST',
                                data: {
                                    uocbh_ocid: restfulParams.oc_id,
                                    uocbh_uid: restfulParams.u_id
                                }
                            },
                            function (err, data) {

                                resultData.addData = data.data;

                                cb();
                            }
                        );
                    }
                } else {
                    cb();
                }

            },
            //更新浏览次数与浏览时间
            function (cb) {
                if (restfulParams.u_id) {
                    if (courseBrowsing.length > 0) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateOrganizationCourseBrowsingHistory,
                                method: 'POST',
                                data: {
                                    uocbh_id: courseBrowsing[0].uocbh_id,
                                    uocbh_create_time: Date.parse(new Date()) * 0.001,
                                    uocbh_count: parseInt(browseTimes[0].uocbh_count) + 1
                                }
                            },
                            function (err, data) {

                                browseTimes = data.data;

                                cb();
                            }
                        );
                    } else {
                        cb();
                    }
                } else {
                    cb();
                }
            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

};
